//
//  SQUserManager.swift
//  SwiftWeibo
//
//  Created by suqianghotel on 16/11/19.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import UIKit
import YYModel

class SQUserModel: NSObject {
    
    var access_token: String?
    var expires_in: String?{
        didSet{
            expiresDate = Date(timeIntervalSinceNow: Double(expires_in!)!)
        }
    }
    var expiresDate: Date?
    var remind_in: String?
    var uid: String?
    
    
    var id:String?
    var screen_name:String?
    var name:String?
    var province:String?
    var city:String?
    var location:String?
//    var description:String?
    var url:String?
    var profile_image_url:String?
    var domain:String?
    var gender:String?
    var followers_count:String?
    var friends_count:String?
    var statuses_count:String?
    var favourites_count:String?
    var created_at:String?
    var following:String?
    var allow_all_act_msg:String?
    var geo_enabled:String?
    var verified:String?
    var allow_all_comment:String?
    var avatar_large:String?
    var verified_reason:String?
    var follow_me:String?
    var online_status:String?
    var bi_followers_count:String?
    
    
    override var description: String{
        return yy_modelDescription()
    }

}

private let filename = "useraccount.json"

class SQUserAccountManager: NSObject {
    
    open var userInfo: SQUserModel? {
        didSet{
            synchronize()
        }
    }

    static let shared: SQUserAccountManager  = SQUserAccountManager()
    
    private override init() {
        
        super.init()
        
        
        
        guard
            let data = try? Data(contentsOf: URL(fileURLWithPath: self.filepath)),
            let jsonObject = try? JSONSerialization.jsonObject(with: data, options: [.allowFragments])

        else {
            
            return
        
        }
        
        userInfo = SQUserModel.yy_model(with: jsonObject as! [AnyHashable : Any])
        
        
        
        if userInfo?.expiresDate?.compare(Date()) != .orderedDescending {
            print("账户过期")
            
            // 清空 token
            userInfo?.access_token = nil
            userInfo?.uid = nil
            
            // 删除帐户文件
            _ = try? FileManager.default.removeItem(atPath: filepath)
        }
    
        
    }
    
    
    
    @discardableResult
    func synchronize() -> Bool
    {
        
        
        if var dict = userInfo?.yy_modelToJSONObject() as? [String: Any] {
            
            dict["expires_in"] = nil;
            
            guard
                let data = try? JSONSerialization.data(withJSONObject: dict, options: [.prettyPrinted]),
                ((try? data.write(to: URL(fileURLWithPath:filepath))) != nil)
            else{
                return false
            }
        
            return true
            
        }
        else
        {
            return false
        }

        
    }
    
    
    lazy var filepath: String = {
        
        let document = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        
        let path = document + "/" + filename
        
        print(path)
        
        return path
    }()
}
